package edu.henu.personnel.controller;

import com.github.pagehelper.PageInfo;
import edu.henu.personnel.entity.*;
import edu.henu.personnel.service.catalog.CatalogService;
import edu.henu.personnel.service.file.FileService;
import edu.henu.personnel.service.notice.NoticeService;
import edu.henu.personnel.service.talent.TalentService;
import edu.henu.personnel.service.teacher.TeacherService;
import edu.henu.personnel.utils.FilePathAndName;
import edu.henu.personnel.utils.FileUtil;
import edu.henu.personnel.vo.NoticeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @program: PersonnelProject
 * @description: 前端界面显示控制器
 * @author: zhuyang
 * @create: 2019-05-22 09:51
 */

@Controller
public class DisplayController {

    @Autowired
    private NoticeService noticeService;

    @Autowired
    private CatalogService catalogService;

    @Autowired
    private TalentService talentService;

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private FileService fileService;

    @GetMapping("/")
    public String toIndexPaget() {
        return "redirect:index.html";
    }

    @GetMapping("/index")
    public String getAllNoticeToDisplay(Model model, HttpSession session) {

        model.addAttribute("tzggNoticeList", noticeService.getNoticeListToIndex(
                catalogService.getCatalogInfoByName("通知公告").getCid()));
        model.addAttribute("xwdtNoticeList", noticeService.getNoticeListToIndex(
                catalogService.getCatalogInfoByName("新闻动态").getCid()));
        model.addAttribute("rcdwNoticeList", noticeService.getNoticeListToIndex(
                catalogService.getCatalogInfoByName("人才队伍").getCid()));
        model.addAttribute("rczpNoticeList", noticeService.getNoticeListToIndex(
                catalogService.getCatalogInfoByName("人才招聘").getCid()));

        session.setAttribute("zzjgList", catalogService.getCatalogInfoByMemo("组织机构"));
        session.setAttribute("rsglList", catalogService.getCatalogInfoByMemo("人事管理"));
        session.setAttribute("szxxList", catalogService.getCatalogInfoByMemo("师资信息"));
        session.setAttribute("lzglList", catalogService.getCatalogInfoByMemo("劳资管理"));
        session.setAttribute("zcycgList", catalogService.getCatalogInfoByMemo("职称与出国"));
        session.setAttribute("bshglList", catalogService.getCatalogInfoByMemo("博士后管理"));
        session.setAttribute("sdsfjsList", catalogService.getCatalogInfoByMemo("师德师风建设"));
        session.setAttribute("fwznList", catalogService.getCatalogInfoByMemo("服务指南"));
        session.setAttribute("zcfgList", catalogService.getCatalogInfoByMemo("政策法规"));
        session.setAttribute("xzzxList", catalogService.getCatalogInfoByMemo("下载中心"));
        return "index";
    }

    @GetMapping("/display/{cid}")
    public String getNoticeByCidToDisplay(@PathVariable("cid") Integer cid,
                                          Model model) {
        PageInfo<NoticeVO> noticeListToDisplay = noticeService.getNoticeListByCatelogid(cid, 1);
        model.addAttribute("noticeList", noticeListToDisplay);

        Catalog catalog = catalogService.getCatalogInfoById(cid);
        model.addAttribute("catalog", catalog);

        List<Catalog> catalogList = catalogService.getCatalogInfoByMemo(catalogService.getCatalogInfoById(cid).getCmemo());
        model.addAttribute("catalogInfoList", catalogList);

        return "second/secondNoticelevel";
    }

    @GetMapping("/display/notice/{nid}")
    public String getNoticeByNidToDisplay(@PathVariable("nid") String nid, Model model) {
        Notice notice = noticeService.getNoticeById(nid);
        Catalog catalog = catalogService.getCatalogInfoById(notice.getCid());

        List<String> filePathList = FileUtil.getFilePath(notice);
        List<FilePathAndName> fileList = FileUtil.getFilePathAndName(filePathList);

        model.addAttribute("notice", notice);
        model.addAttribute("catalog", catalog);
        model.addAttribute("fileList", fileList);

        // 增加访问量
        noticeService.addNoticeBrowse(nid);

        return "third/noticePage";
    }

    @GetMapping("/display/talent/{cid}")
    public String toTalentTable(@PathVariable("cid") Integer cid, Model model) {
        List<Talent> talentList = talentService.getTalentList();
        model.addAttribute("talentList", talentList);

        Catalog catalog = catalogService.getCatalogInfoById(cid);
        model.addAttribute("catalog", catalog);

        List<Catalog> catalogList = catalogService.getCatalogInfoByMemo(catalogService.getCatalogInfoById(cid).getCmemo());
        model.addAttribute("catalogInfoList", catalogList);

        return "second/secondTalentlevel";
    }

    @GetMapping("/display/talentList/{ttid}")
    public String toTeachersTable(@PathVariable("ttid") Integer ttid, Model model) {
        List<Teacher> teacherList = teacherService.getTeacherInTheTelnetInfo(ttid);
        model.addAttribute("teacherList", teacherList);

        Catalog catalog = catalogService.getCatalogInfoByName("人才队伍");
        model.addAttribute("catalog", catalog);

        List<Catalog> catalogList = catalogService.getCatalogInfoByMemo("师资信息");
        model.addAttribute("catalogInfoList", catalogList);

        return "second/teachers";
    }

    @GetMapping("/display/teacher/{tid}")
    public String toTeacherPage(@PathVariable("tid") String tid, Model model) {
        Teacher teacher = teacherService.getTeacherById(tid);

        teacherService.addTeacherBrowse(teacher.getTid());

        model.addAttribute("teacher", teacher);

        return "third/teacherPage";
    }


    @GetMapping("/display/download/{cid}")
    public String toDownloadTable(@PathVariable("cid")Integer cid, Model model) {

        PageInfo<DownLoadFile> files = fileService.getFilesByCid(cid, 1);
        model.addAttribute("files", files);

        Catalog catalog = catalogService.getCatalogInfoById(cid);
        model.addAttribute("catalog", catalog);

        List<Catalog> catalogList = catalogService.getCatalogInfoByMemo(catalogService.getCatalogInfoById(cid).getCmemo());
        model.addAttribute("catalogInfoList", catalogList);
        return "second/secondDownlevel";
    }

    @GetMapping("/display/notice/page/{cid}/{pageNum}")
    public String toGetNoticeList(@PathVariable("cid") Integer cid,
                                  @PathVariable("pageNum") Integer pageNum,
                                  Model model) {

        PageInfo<NoticeVO> noticeListToDisplay = noticeService.getNoticeListByCatelogid(cid, pageNum);
        model.addAttribute("noticeList", noticeListToDisplay);

        Catalog catalog = catalogService.getCatalogInfoById(cid);
        model.addAttribute("catalog", catalog);

        List<Catalog> catalogList = catalogService.getCatalogInfoByMemo(catalogService.getCatalogInfoById(cid).getCmemo());
        model.addAttribute("catalogInfoList", catalogList);

        return "second/secondNoticelevel";
    }


    @GetMapping("/display/file/page/{cid}/{pageNum}")
    public String toGetFileList(@PathVariable("cid") Integer cid,
                                @PathVariable("pageNum") Integer pageNum,
                                Model model) {
        PageInfo<DownLoadFile> files = fileService.getFilesByCid(cid, 1);
        model.addAttribute("files", files);

        Catalog catalog = catalogService.getCatalogInfoById(cid);
        model.addAttribute("catalog", catalog);

        List<Catalog> catalogList = catalogService.getCatalogInfoByMemo(catalogService.getCatalogInfoById(cid).getCmemo());
        model.addAttribute("catalogInfoList", catalogList);

        return "second/secondDownlevel";
    }

}
